// @preview-file on clear nolog
import { React, toNode, useRef } from 'DoraX';
import { Color, DrawNode, Node, Vec2, cycle, thread } from 'Dora';
import { designSize } from 'Script/config';

export interface TransitionProps {
	root: Node.Type;
	color?: Color.Type;
	time?: number;
	type?: number;
	waittime?: number;
}

export const Transition = (props: TransitionProps) => {
	const {
		root = Node(),
		color = Color(0xff000000),
		time = 1,
		type = 0,
		waittime = 1,
	} = props;
	// const root = Node();
	const drawnode = DrawNode();
	root.addChild(drawnode);
	drawnode.order = 1;
	let tempcolor = Color(color.toARGB());
	thread(() => {
		cycle(time, function (scale: number) {
			if (type == -1) {
				tempcolor.a = Math.floor(color.a * (1 - scale));
			} else if (type == -1) {
				tempcolor.a = Math.floor(color.a * scale);
			} else {
				if (scale < 0.5)
					tempcolor.a = Math.floor(color.a * (scale * 2));
				else
					tempcolor.a = Math.floor(color.a * (2 - 2 * scale));
			}
			drawnode.clear();
			drawnode.drawPolygon([Vec2(designSize.width / 2, designSize.height / 2), Vec2(-designSize.width / 2, designSize.height / 2), Vec2(-designSize.width / 2, -designSize.height / 2), Vec2(designSize.width / 2, -designSize.height / 2)], tempcolor);
			if (scale === 1) {
				root.removeChild(drawnode);
			}
		});
	});
	cycle(waittime,function (a){});
}